<?php

namespace app\modules\clinical\controllers;

use Yii;
use app\modules\clinical\models;
use yii\data\ActiveDataProvider;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\AccessControl;
use app\components\PtAccessControl;

/**
 * ExcelController implements the CRUD actions for Crtinfo model.
 */
class ExcelController extends Controller
{
    public function behaviors()
    {
        return [
            // 'access' => [
            //     'class' => AccessControl::className(),
            //     'rules' => [
            //         [
            //             'allow' => true,
            //             'roles' => ['@'],
            //         ],
            //     ],
            // ],
            'pt_access' => [
                'class' => PtAccessControl::className(),
            ]
        ];
    }

    public function actionIndex()
    {
        $pt_id = Yii::$app->session->get('pt_id');
        $dataProvider = new ActiveDataProvider([
            'query' => models\Crtinfo::find()->where(['delete_at' => 0])->andWhere(['or', 'pt_id='.$pt_id, 'rel_id='.$pt_id]),
            'sort' => [
                'defaultOrder' => [
                    'crt_time' => SORT_DESC,
                ]
            ],
        ]);

        return $this->render('index', [
            'dataProvider' => $dataProvider,
            'pt_rel_list' => models\Patient::findOne(Yii::$app->session->get('pt_id'))->getRelations(),
        ]);
    }

    public function actionView($id)//crtid
    {
        $model = $this->findCrt($id);
        
        if (!isset($model->excel->pk_id)) {
            throw new NotFoundHttpException('该表部分信息丢失，请联系管理员');
        }

        return $this->render('view', [
            'model' => $model,
            'pt_rel_list' => models\Patient::findOne(Yii::$app->session->get('pt_id'))->getRelations(),
        ]);
    }

    public function actionCreate()
    {
        $type = Yii::$app->request->get('type', false);
        $model = $this->getModel($type);
        $model->defaultValue();

        if ($model->load(Yii::$app->request->post()) && $model->save()) {

            $evaluation_time = Yii::$app->request->post('evaluation_time', NULL);
            $pt_rel = Yii::$app->request->post('pt_rel');

            $crtid = models\Crtinfo::create($type, $model->pk_id, $pt_rel, $evaluation_time);

            return $this->redirect(['/clinical/excel/view/', 'id' => $crtid, 'type' => $type]);
        }

        return $this->render('create', [
            'model' => $model,
            'type' => $type,
            'pt_rel_list' => $this->getRelatives(),
            'error' => $model->errors,
        ]);
    }

    public function actionUpdate($id)//crtid
    {
        $type = Yii::$app->request->get('type', false);
        $post = Yii::$app->request->post();
        $crt = $this->findCrt($id);
        $model = $crt->excel;

        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            $crt->evaluation_time = Yii::$app->request->post('evaluation_time', NULL);
            $crt->save();
            return $this->redirect(['/clinical/excel/view/', 'id' => $id, 'type' => $type]);
        }

        return $this->render('update', [
            'model' => $model,
            'crtid' => $id,
            'type' => $crt->pt_diagnose,
            'pt_rel_list' => $this->getRelatives(),
            'pt_rel' => ($crt->rel_id != 0) ? $crt->pt_id : 0,
            'evaluation_time' => $crt->evaluation_time,
        ]);
    }

    public function actionDelete($id)
    {
        $type = Yii::$app->request->get("type", 0);

        $model = models\Crtinfo::findOne($id);
        $model->delete_at = strtotime(date("Y-m-d H:i:s"));
        if ($model->save()) {
            return $this->redirect('/clinical/excel');
        }

        return $this->redirect('/clinical/excel/view?id='.$id.'&type='.$type);
    }

    protected function findCrt($id)
    {
        $model = models\Crtinfo::find()->where(['pk_id' => $id])->with('excel')->one();

        if ($model !== null) {
            return $model;
        } else {
            throw new NotFoundHttpException('The requested page does not exist.');
        }
    }

    protected function getModel($type)
    {
        switch ($type) {
            case 'pd': $model = new models\ExcelPd(); break;

            case 'pdf': $model = new models\ExcelPdf(); break;

            case 'et': $model = new models\ExcelEt(); break;

            case 'sca': $model = new models\ExcelSca(); break;

            case 'cmt': $model = new models\ExcelCmt(); break;

            case 'hsp': $model = new models\ExcelHsp(); break;

            case 'pfbc': $model = new models\ExcelPfbc(); break;
            
            case 'fcmte': $model = new models\ExcelFcmte(); break;

            case 'udprs': $model = new models\ExcelUdprs(); break;

            case 'udprs2': $model = new models\ExcelUdprs2(); break;

            case 'pdx': $model = new models\ExcelPdx(); break;
            
            default: throw new NotFoundHttpException('请选择表格类型'); break;
        }

        return $model;
    }

    protected function getRelatives()
    {
        $rel = models\Patient::findOne(Yii::$app->session->get('pt_id'))->getRelatives();
        return $rel;
    }
}
